﻿using Direct.IDAL.Message;
using DirectSell.DAL.User.Message;
using DirectSell.Model.Message;
using DirectSell.Model.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectSell.DAL.Manager.MessageManage
{
    /// <summary>
    /// 后台留言管理
    /// </summary>
    public class MessageManageRepository:BaseManagerRepository, IMessageManageRepository
    {
        #region 查询发送给我的留言
        /// <summary>
        /// 查询发送给我的留言
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<IList<Messages>> MyMessage(BaseCondition condition)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" SELECT * FROM Messages WHERE ReciveCode='{0}'  ", "admin");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat(" AND (Title LIKE '%{0}%' OR MContent  LIKE '%{0}%' OR SendCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='2010-01-01' and CreateDate<='{1}' ", condition.endTime);
                }
            }
            sb.Append(" ORDER BY State ASC ");
            var result = await base.QueryAsync<Messages>(sb.ToString());
            return result as IList<Messages>;
        }
        #endregion

        #region 查询我发出的留言
        /// <summary>
        /// 查询我发出的留言
        /// </summary>
        /// <param name="usercode"></param>
        public async Task<IList<Messages>> AlreadyMessage(BaseCondition condition)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" SELECT * FROM Messages WHERE SendCode='{0}'  ", "admin");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat(" AND (Title LIKE '%{0}%' OR MContent  LIKE '%{0}%' OR SendCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND CreateDate>='{0}' and CreateDate<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND CreateDate>='2010-01-01' and CreateDate<='{1}' ", condition.endTime);
                }
            }
            sb.Append(" ORDER BY State ASC ");
            var result = await base.QueryAsync<Messages>(sb.ToString());
            return result as IList<Messages>;
        }
        #endregion

        //#region 查询是否有新留言
        ///// <summary>
        ///// 查询是否有新留言
        ///// </summary>
        ///// <returns></returns>
        //public bool QueryNewMsg()
        //{

        //}
        //#endregion

    }
}
